package com.saishi.servlet;

import com.saishi.dao.ClassesDao;
import com.saishi.dao.impl.ClassesDaoImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import javax.swing.*;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Date;
import java.util.List;

@WebServlet("/class/*")
public class ClassesServlet extends BaseServlet {
    ClassesDao dao=new ClassesDaoImpl();
    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        List list=dao.selectAll();
        request.setAttribute("list",list);
            request.getRequestDispatcher("/classindex.jsp").forward(request,response);
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String cname=request.getParameter("cname");
        String cgrade=request.getParameter("cgrade");
        String cstate=request.getParameter("cstate");
        Date ccreatime= Date.valueOf(request.getParameter("ccreatime"));
        int teacherid=Integer.parseInt(request.getParameter("teacherid"));
        dao.add(cname,cgrade,cstate,ccreatime,teacherid);
        response.sendRedirect("/class/selectAll");
    }

    public void selectAllToJsp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        List list=dao.selectAll();
        request.setAttribute("list",list);
        request.getRequestDispatcher("/ClassFind.jsp").forward(request,response);
    }
    public void selectAllToDelJsp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        List list=dao.selectAll();
        request.setAttribute("list",list);
        request.getRequestDispatcher("/ClassDel.jsp").forward(request,response);
    }

    public void selectDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        int id=Integer.parseInt(request.getParameter("id"));
        dao.del(id);
        response.sendRedirect("/class/selectAll");
    }
    public void selectId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {

        List list=dao.selectId();
        request.setAttribute("list",list);
        request.getRequestDispatcher("/ClassUpdate.jsp").forward(request,response);
    }

    public void selectById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {

        int id= Integer.parseInt(request.getParameter("id"));
        List list=dao.selectById(id);
        request.setAttribute("list",list);
        request.getRequestDispatcher("/ClassUp2.jsp").forward(request,response);
    }

    public void Update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {

        int id= Integer.parseInt(request.getParameter("cid"));
        String cname=request.getParameter("cname");
        String cgrade=request.getParameter("cgrade");
        String cstate=request.getParameter("cstate");
        Date ccreatime= Date.valueOf(request.getParameter("ccreatime"));
        int teacherid=Integer.parseInt(request.getParameter("teacherid"));
//        System.out.println(id+cname+cgrade+cstate+ccreatime+teacherid);
        dao.Update(id,cname,cgrade,cstate,ccreatime,teacherid);
        response.sendRedirect("/class/selectAll");
    }

}
